<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>快速排序</title>
</head>
<body>
    <script>

        var arr = [23,545,65,78,2,56,45,7,4,34,56]
        function quiteSort(arr){
            //1、取一个数，将他与数组中的元素对比，小的放到左边的数组中，大的放到右边的数组中
            let baseNum = arr[0];
            let leftArry = [];
            let rightArry = [];
            // debugger
            arr.forEach((item,index)=>{
                if(index>0){
                    if(item<baseNum){
                        leftArry.push(item)
                    }else {
                        rightArry.push(item)
                    }
                }
            })
            //2、递归左右两边的数组，当leng小于2时停止递归
            if(leftArry.length>1){
                leftArry = quiteSort(leftArry)
            }
            if(rightArry.length>1){
                rightArry = quiteSort(rightArry)
            }
            //3、将左右数组连接起来
            return [...leftArry,baseNum,...rightArry]
        }
        let array = quiteSort(arr)
        console.log(array)
    </script>
</body>
</html>